home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / dlgds411.zip / CPPSRC1.CPP < prev    next >
C/C++ Source or Header  |  1994-01-07  |  15KB  |  591 lines

  1. #define Uses_TStringCollection
  2. #include <tv.h>
  3. #include <stdlib.h>
  4. #include <dos.h>
  5. #include <dir.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <stdio.h>
  11. #include "readscpt.h"
  12.  
  13. ofstream outf;
  14. char DlgName[50];   //holds the dialog varible name for easy access
  15. Boolean needcontrol1;  //set if need control1 variable name
  16.  
  17. char* quoted(const char* s)
  18. //if the first char of s is '@', assumes a variable wanted and strips the '@'
  19. //else returns a double quoted string or 0 for empty string
  20. {
  21.  static char q[300];
  22.  if (s[0] == '@')
  23.    {strcpy(q, &s[1]);
  24.     return q;
  25.    }
  26.  strcpy(q, "\"");   //the first quote in place
  27.  if (*s == '\0' || !s) return "0";
  28.  strcat(q, s);
  29.  short l = strlen(q);
  30.  q[l] = '\"';
  31.  q[l+1] = '\0';
  32.  return q;
  33. }
  34.  
  35. void aField(ViewObj *P, char* fieldtype)
  36. {
  37.  outf << "  " << fieldtype << " " << P->FieldName << ";   //" << P->Obj <<endl;
  38. }
  39.  
  40. void aVar(ViewObj *P)
  41. {
  42.  if (strcmp(P->VarName, "control") != 0)
  43.    outf << P->Obj << " *" << P->VarName << ";\n";
  44. }
  45.  
  46. //the following extend the ViewObj struct's to also write the code
  47. struct DialogWriteObj : DialogObj {
  48.      virtual void writeCode();
  49.      };
  50.  
  51. struct ButtonWriteObj : ButtonObj {
  52.      virtual void writeCode();
  53.      virtual void writeVars() {aVar(this);};
  54.      };
  55.  
  56. struct InputLongWriteObj : InputLongObj {
  57.      virtual void writeCode();
  58.      virtual void writeFields() {aField(this, "long");} ;
  59.      virtual void writeVars() {aVar(this);};
  60.      };
  61. struct LabelWriteObj : LabelObj {
  62.      virtual void writeCode();
  63.      };
  64. struct HistoryWriteObj : HistoryObj {
  65.      virtual void writeCode();
  66.      };
  67. struct InputLineWriteObj : InputLineObj {
  68.      virtual void writeCode();
  69.      virtual void writeFields();
  70.      virtual void writeVars() {aVar(this);};
  71.      };
  72. struct ClusterWriteObj : ClusterObj {
  73.      virtual void writeCode();
  74.      virtual void writeFields() {aField(this, "ushort");} ;
  75.      virtual void writeVars() {aVar(this);};
  76.      };
  77. struct ListBoxWriteObj : ListBoxObj {
  78.      virtual void writeCode();
  79.      virtual void writeFields() {aField(this, "TListBoxRec");} ;
  80.      virtual void writeVars() {aVar(this);};
  81.      };
  82. struct ScrollBarWriteObj : ScrollBarObj {
  83.      virtual void writeCode();
  84.      virtual void writeVars();
  85.      };
  86. struct MemoWriteObj : MemoObj {
  87.      virtual void writeCode();
  88.      virtual void writeFields();
  89.      virtual void writeVars() {aVar(this);};
  90.      };
  91. struct StaticTextWriteObj : StaticTextObj {
  92.      virtual void writeCode();
  93.      virtual void writeVars() {aVar(this);};
  94.      };
  95. struct ColoredTextWriteObj : ColoredTextObj {
  96.      virtual void writeCode();
  97.      virtual void writeVars() {aVar(this);};
  98.      };
  99.  
  100.  
  101. //now that we know the final extensions of ViewObj, we can write the
  102. //getKind function
  103. ViewObj *getKind(recType Kind)
  104. { ViewObj *P;
  105.   switch (Kind) {
  106.       case Dlg : P = new DialogWriteObj(); break;
  107.       case Button : P = new ButtonWriteObj(); break;
  108.       case InputL : P = new InputLineWriteObj(); break;
  109.       case Labl : P = new LabelWriteObj();  break;
  110.       case Histry : P = new HistoryWriteObj(); break;
  111.       case ILong : P = new InputLongWriteObj(); break;
  112.       case CheckB: P = new ClusterWriteObj(); break;
  113.       case RadioB: P = new ClusterWriteObj(); break;
  114.       case MultiCB: P = new MultiCheckBoxObj(); break;
  115.       case ListB: P = new ListBoxWriteObj(); break;
  116.       case ScrollB :  P = new ScrollBarWriteObj(); break;
  117.       case Memo: P = new MemoWriteObj(); break;
  118.       case SText: P = new StaticTextWriteObj(); break;
  119.       case CText : P = new ColoredTextWriteObj(); break;
  120.       default : P = 0; break;
  121.       }
  122. return P;
  123. }
  124.  
  125. char * getWinFlagWords(ushort w, char *s)
  126. //given the set bits return names in 'or' form
  127. {
  128.  static char *flagArray[4] =  {
  129.         "wfMove", "wfGrow", "wfClose", "wfZoom"};
  130.  s[0] = '\0';
  131.  for (int i = 0; i <= 3; i++) {
  132.    if ((w & 1) == 1 && flagArray[i][0] != '\0')
  133.      strcat(strcat(s, flagArray[i]), " | ");
  134.    w >>= 1;
  135.    }
  136.  int l = strlen(s);
  137.  if (l > 4)
  138.     s[l-3] = '\0';   //remove last " | "
  139.  return s;
  140. }
  141.  
  142. char * getEventWords(ushort w, char *s)
  143. //given the set bits return names in 'or' form
  144. {
  145.  static char *flagArray[16] =  {
  146.         "evMouseDown", "evMouseUp", "evMouseMove", "evMouseAuto",
  147.     "evKeyDown", "0x20", "0x40", "0x80", "evCommand", "evBroadcast",
  148.     "0x400", "0x800", "0x1000", "0x2000", "0x4000", "0x8000"};
  149.  s[0] = '\0';
  150.  for (int i = 0; i <= 15; i++) {
  151.    if ((w & 1) == 1 && flagArray[i][0] != '\0')
  152.      strcat(strcat(s, flagArray[i]), " | ");
  153.    w >>= 1;
  154.    }
  155.  int l = strlen(s);
  156.  if (l > 4)
  157.     s[l-3] = '\0';   //remove last " | "
  158.  return s;
  159. }
  160.  
  161. char * getOptionWords(ushort w, char *s)
  162. //given the set bits return names in 'or' form
  163. {
  164.  static char *flagArray[16] =  {
  165.         "ofSelectable", "ofTopSelect", "ofFirstClick", "ofFramed",
  166.         "ofPreProcess", "ofPostProcess", "ofBuffered", "ofTileable",
  167.         "ofCenterX", "ofCenterY", "ofValidate", "0x800", "0x1000",
  168.         "0x2000", "0x4000", "ofShoehorn"};
  169.  s[0] = '\0';
  170.  for (int i = 0; i <= 15; i++) {
  171.    if ((w & 1) == 1 && flagArray[i][0] != '\0')
  172.      strcat(strcat(s, flagArray[i]), " | ");
  173.    w >>= 1;
  174.    }
  175.  int l = strlen(s);
  176.  if (l > 4)
  177.     s[l-3] = '\0';   //remove last " | "
  178.  return s;
  179. }
  180.  
  181. short bitCount(ushort w) //numbers of set bits in the word
  182. {
  183.  short count = 0;
  184.  for (int i = 0; i <= 15; i++)  {
  185.     if ((w & 1) == 1)
  186.        count++;
  187.     w >>= 1;
  188.     }
  189. return count;
  190. }
  191.  
  192. void doBitOutput(const char* var, const char* pre, ushort actual,
  193.                        ushort defaul, char* (func)(ushort w, char* s))
  194. //output something like "foo->options |= ofSelectable or ofFramed;"
  195. //and/or "foo-> options &= ofTopSelect;"
  196. //actual is the bits set and defaul are the default settings
  197. //it's known that actual and defaul are not equal on entry
  198. {
  199.  char s[100];
  200.  ushort NOTs, ORs, diff ;
  201.  diff = actual ^ defaul;  //the bits that are different
  202.  if (bitCount(diff) > 5) { //this is too complex--output hex number
  203.    outf << var << pre << " = 0x" << hex << actual << dec <<";\n";
  204.    return;
  205.    }
  206.  NOTs = diff & defaul;  //the bits not in defaul
  207.  ORs = diff & actual;    //the extra bits in actual
  208.  s[0] = '\0';
  209.  if (NOTs != 0) {
  210.    outf << var <<pre;
  211.    if (bitCount(NOTs) == 1)
  212.      outf << " &= ~" << func(NOTs, s) << ";\n";
  213.    else
  214.      outf << " &= ~(" << func(NOTs, s) << ");\n";
  215.    }
  216.  s[0] = '\0';
  217.  if (ORs != 0)
  218.    outf << var << pre << " |= " << func(ORs, s) << ";\n";
  219. }
  220.  
  221. void doTRect(ViewObj *P)
  222. {
  223. outf << "(TRect(" << P->X1 << ", " << P->Y1 << ", "
  224.      << P->X2 << ", " << P->Y2 << ")";
  225. }
  226.  
  227. void insertControl(const char *Name)
  228. {
  229.  outf <<  DlgName << "->insert(" << Name << ");\n\n";
  230. }
  231.  
  232. void doOpEvent(ViewObj *P)
  233. {if (P->DefEvMsk != P->EvMsk)
  234.    doBitOutput(P->VarName, "->eventMask", P->EvMsk, P->DefEvMsk, getEventWords);
  235.  
  236. if (P->DefOptns != P->Optns)
  237.    doBitOutput(P->VarName, "->options", P->Optns, P->DefOptns, getOptionWords);
  238. }
  239.  
  240. void writeHelpCtx(char * VarName, char* H, ushort Ctx)
  241. {
  242. if (*H != '\0') {
  243.   if (strcmp(H, "hcNoContext")) {
  244.     outf << VarName << "->helpCtx = " << H << ";\n";
  245.     }
  246.   }
  247. else if (Ctx != 0) {
  248.     outf << VarName << "->helpCtx = " << Ctx << ";\n";
  249.     }
  250. }
  251.  
  252. void start(ViewObj *P)
  253. {
  254.   outf << P->VarName << " = new " << P->Obj;
  255.   doTRect(P);
  256. }
  257.  
  258. void finish(ViewObj *P)
  259. {
  260.   writeHelpCtx(P->VarName, P->HelpCtxSym, P->HCtx);
  261.  doOpEvent(P);
  262.  insertControl(P->VarName);
  263. }
  264.  
  265. void DoControls(void *p, void*)
  266. {ViewObj *P = (ViewObj*)p;
  267.  P->writeCode();
  268. }
  269.  
  270. void DialogWriteObj::writeCode()
  271. {
  272.   strncpy(DlgName, VarName, 49);
  273.  
  274.   outf << Obj << "* " << DlgFuncName << "(void)\n{\n";
  275.   outf << "TView *control";
  276.   if (needcontrol1) outf << ", *control1;\n";
  277.   else outf << ";\n";
  278.  
  279.   outf << Obj << "* " << VarName << " = new " << Obj;
  280.   doTRect(this);
  281.   outf << ", " << quoted(Title) << ");\n";
  282.   outf << "if (!" << VarName << ") return 0;\n\n";
  283.  
  284.   doOpEvent(Dialog);
  285.   if (WinFlags != 5)    //5 is the default
  286.      doBitOutput(VarName, "->flags", WinFlags,